#include "repairmanagewidget.h"
#include "ui_repairmanagewidget.h"
#include "repairapplywidget.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include "repairratewidget.h"
#include "loginwidget.h"

RepairManageWidget::RepairManageWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RepairManageWidget)
{
    ui->setupUi(this);
    initWindow();
    initTable();
}

RepairManageWidget::~RepairManageWidget()
{
    delete ui;
}

void RepairManageWidget::flushTable()
{
    initTable();
}

void RepairManageWidget::initWindow()
{
    this->setWindowTitle("维修管理");

    if (LoginWidget::userRole == 2)
    {
        ui->btn_repair_apply->hide();
        ui->btn_repair_rate->hide();
        ui->btn_ready->move(550, 40);
        ui->btn_finish->move(550, 140);
    }
    else if (LoginWidget::userRole == 3)
    {
        ui->btn_ready->hide();
        ui->btn_finish->hide();
        ui->btn_repair_apply->move(550, 40);
        ui->btn_repair_rate->move(550, 140);
    }

}

void RepairManageWidget::initTable()
{
    model = new QSqlTableModel(this);
    model->setTable("repair_apply"); //指定表
    model->setHeaderData(1,Qt::Horizontal,"报修人");
    model->setHeaderData(2,Qt::Horizontal,"报修信息");
    model->setHeaderData(3,Qt::Horizontal,"报修进度");
    model->setHeaderData(4,Qt::Horizontal,"评价");
    model->setHeaderData(5,Qt::Horizontal,"是否缴费");
    model->select(); //查询
    model->setEditStrategy(QSqlTableModel::OnManualSubmit); //Model编辑策略，提交生效
    ui->tableView->setModel(model); //将模型设置到视图中
    ui->tableView->setColumnHidden(0, 1); //隐藏编号列
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);   //使其不可编辑
}

/**
 * @brief 故障报修
 * 使用者：业主 Role=3
 */
void RepairManageWidget::on_btn_repair_apply_clicked()
{
    RepairApplyWidget *widget = new RepairApplyWidget();
    connect(widget, SIGNAL(closeAndFlush()), this, SLOT(flushTable()));
    widget->show();
}

/**
 * @brief 维修评价
 * 使用者：业主 Role=3
 */
void RepairManageWidget::on_btn_repair_rate_clicked()
{
    int curRow = ui->tableView->currentIndex().row(); //获取选中行号
    QString repairProcess = model->itemData(model->index(curRow, 3))[0].toString();
    int id = model->itemData(model->index(curRow, 0))[0].toInt();
    if (repairProcess == "已维修")
    {
        RepairRateWidget *widget = new RepairRateWidget();
        widget->setRepairId(id);
        connect(widget, SIGNAL(closeAndFlush()), this, SLOT(flushTable()));
        widget->show();
    }
    else
    {
        QMessageBox::warning(this, tr("错误"), tr("更新失败"));
    }
}

/**
 * @brief 准备维修
 * 使用者：物业工作人员 Role=2
 */
void RepairManageWidget::on_btn_ready_clicked()
{
    int curRow = ui->tableView->currentIndex().row(); //获取选中行号
    QString repairProcess = model->itemData(model->index(curRow, 3))[0].toString();
    int id = model->itemData(model->index(curRow, 0))[0].toInt();
    if (repairProcess == "待维修")
    {
        QSqlQuery query;
        query.prepare("UPDATE repair_apply SET repair_process='维修中' WHERE id = :id");
        query.bindValue(":id", id);
        if (!query.exec())
        {
            qDebug() << query.lastError();
        }
        else
        {
            QMessageBox::information(this, tr("提示"), tr("更新成功"));
        }
    }
    else
    {
        QMessageBox::warning(this, tr("错误"), tr("更新失败"));
    }
    initTable();
}

/**
 * @brief 完成维修
 * 使用者：物业工作人员 Role=2
 */
void RepairManageWidget::on_btn_finish_clicked()
{
    int curRow = ui->tableView->currentIndex().row(); //获取选中行号
    QString repairProcess = model->itemData(model->index(curRow, 3))[0].toString();
    int id = model->itemData(model->index(curRow, 0))[0].toInt();
    if (repairProcess == "维修中")
    {
        QSqlQuery query;
        query.prepare("UPDATE repair_apply SET repair_process='已维修' WHERE id = :id");
        query.bindValue(":id", id);
        if (!query.exec())
        {
            qDebug() << query.lastError();
        }
        else
        {
            QMessageBox::information(this, tr("提示"), tr("更新成功"));
        }
    }
    else
    {
        QMessageBox::warning(this, tr("错误"), tr("更新失败"));
    }
    initTable();
}
